<?php
class GoodAction extends LoginAction
{
	private $type = 'good';
	public function index ()
	{
		//获取查询条件
		$map = array();
		$brand = intval(getGet('brand'));
		$pro = intval(getGet('pro'));
		$name = trim(getGet('name'));
		$material = intval(getGet('material'));
		if ($brand) {
			$map['m_good.bid'] = $brand;
		}
		if ($pro) {
			$map['m_good.pid'] = $pro;
		}
		if ($material) {
			$map['m_good.material'] = $material;
		}
		if ($name) {
			$map['m_good.name'] = array('like', '%' . $name . '%');
		}
		$this->getOtherData();
		$GoodModel = D('Good');
		$goods = $GoodModel->getGoodALlProPage($map);
		$this->assign('brandId', $brand);
		$this->assign('proId', $pro);
		$this->assign('materialId', $material);
		$this->assign('name', $name);
		$this->assign('page', $goods['page']);
		$this->assign('goods', $goods['data']);
		$this->display();
	}
	/**
	 * 添加商品操作
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function add ()
	{
		//获取需要数据
		$this->getOtherData();
		if (isPost()) {
			$GoodModel = D('Good');
			$data = $this->getGoodPostData();
			$goodId = $this->createAdd('Good', $data);
			//TODO 添加失败删除原图
		}
		$this->display();
	}
	/**
	 * 修改商品操作
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function edit ()
	{
		$id = (int) getGetOrPost('id');
		$map = array();
		$map['id'] = $id;
		$GoodModel = M('Good');
		$good = $GoodModel->where($map)->find();
		if (! $good) {
			$this->error('数据传输有错误！');
		} else {
			if (isPost()) {
				$data = $this->getGoodPostData(true, $id);
				$data['id'] = $id;
				//$GoodModel->data($data)->save()
				$rs = $this->createUpdate('Good', $data, false);
				if ($rs) {
					$this->successMessage('修改成功！');
					//修改成功删除原有图片
					if (($good['image'] != $data['image']) && $data['image']) {
						$this->delUpLoadFile($good['image'], $this->type);
					}
					//重新加载品牌信息
					$good = $GoodModel->where($map)->find();
				} else {
					$this->errorMessage('修改失败！');
				}
				//$good = $Good->where($map)->find();
			}
			$this->assign('good', $good);
		}
		//获取需要数据
		$this->getOtherData();
		//加载品牌图片
		$imageUrl = $this->getManagedUrl($good['image'], $this->type);
		$this->assign('imageUrl', $imageUrl);
		$this->display();
	}
	/**
	 * 执行商品参数获取和判断
	 * @param blooen $isUpload
	 * @param blooen $isEdit
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function getGoodPostData ($isEdit = false, $id = 0)
	{
		//TODO 添加传递参数判断
		$GoodModel = D('Good');
		$map = array();
		//参数获取
		$goodName = trim(getPost('gName'));
		$aNumber = trim(getPost('aNumber')); //货号
		$brand = getPost('brand');
		$goodPro = getPost('goodPro');
		$material = getPost('material');
		$material = getPost('material');
		$goodIntr = getPost('goodIntr');
		$goodPrice = getPost('gPrice');
		//判断处理
		//名称唯一判断
		$status = $GoodModel->theOnly($goodName, 'name', '名称',
				true, array('id' => $id));
		if (! $status['status']) {
			$this->error($status['doc']);
		}
		//货号唯一判断
		if ($aNumber) {
			$status = $GoodModel->theOnly($aNumber, 'article_number', '货号',
					true, array('id' => $id));
			if (! $status['status']) {
				$this->error($status['doc']);
			}
		}
		// @TODO 如果插入失败执行删除文件操作
		$data = array();
		$data['bid'] = $brand;
		$data['pid'] = $goodPro;
		$data['name'] = $goodName;
		$data['article_number'] = $aNumber;
		$data['material'] = $material;
		$data['price'] = $goodPrice;
		$data['intr'] = $goodIntr;
		$newTime = time();
		if (! $isEdit) {
			$data['create_time'] = $newTime;
		}
		$data['update_time'] = $newTime;
		//文件执行上传并且保存返回媒体库标示
		$doUpload = true;
		if ($isEdit) {
			if (! $_FILES['image']['name']) {
				$doUpload = false;
			}
		}
		if ($doUpload) {
			$fId = $this->uploadFiles($this->type);
			$data['image'] = $fId; //媒体库标示
			if(!$data['image']){
				$this->error('图片传送失败');
			}
		}
		return $data;
	}
	/**
	 * 获取商品需要的其他数据基本数据
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function getOtherData ()
	{
		//品牌
		$BrandModel = D('Brand');
		$brand = $BrandModel->order('initials ASC')->select();
		//商品属性
		$MaterialModel = M('Material');
		$material = $MaterialModel->select();
		//材质
		$GoodProModel = M('GoodPro');
		$goodPro = $GoodProModel->select();
		//赋值
		$this->assign('brand', $brand);
		$this->assign('material', $material);
		$this->assign('goodPro', $goodPro);
	}
	/**
	 * 商品信息预览效果
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function preview ()
	{
		$id = (int) getGetOrPost('id');
		$map = array();
		$map['id'] = $id;
		$GoodModel = M('Good');
		$good = $GoodModel->where($map)->find();
		//获取需要数据
		$map = array();
		$map['id'] = $good['bid'];
		$this->setBaseData('Brand', $map, 'brand');
		unset($map);
		$map['id'] = $good['material'];
		$this->setBaseData('Material', $map, 'material');
		unset($map);
		$map['id'] = $good['pid'];
		$this->setBaseData('GoodPro', $map, 'goodPro');
		//加载品牌图片
		$imageUrl = $this->getManagedUrl($good['image'], $this->type);
		$this->assign('imageUrl', $imageUrl);
		$this->assign('good', $good);
		$this->display();
	}
}
?>